package net.oschina.arvin.dbjava.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Arvin
 * @time 2017/9/28 16:43
 */
public class Utils {

    private static final Set<String> javaKeywords = new HashSet<String>();

    static {
        javaKeywords.addAll(Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "strictfp", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"));
    }

    public static boolean isBlank(String value) {
        return null == value || "".equals(value.trim());
    }

    public static boolean isNotBlank(String value) {
        return null != value && !"".equals(value.trim());
    }

    /**
     * 是否java关键字
     *
     * @param str
     * @return
     */
    public static boolean isJavaKeyword(String str) {
        return javaKeywords.contains(str);
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String firstLetterUpperCase(String str) {
        if (str.length() == 1) {
            return str.toUpperCase();
        }
        return String.valueOf(str.charAt(0)).toUpperCase() + str.substring(1);
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     */
    public static String firstLetterLowerCase(String str) {
        if (str.length() == 1) {
            return str.toLowerCase();
        }
        return String.valueOf(str.charAt(0)).toLowerCase() + str.substring(1);
    }

    public static Integer toInt(String intStr, Integer def) {
        if (null == intStr || "".equals(intStr.trim())) {
            return null;
        }
        try {
            return Integer.parseInt(intStr);
        } catch (NumberFormatException e) {
            return def;
        }
    }
}
